package com.zwface.demo.greendao;

import com.zwface.demo.App;
import com.zwface.demo.model.db.JoinUserWithCourse;

import java.util.List;

/**
 * created by 张中文
 * on 2020/5/4
 * 描述：用户与课程关联表管理
 */
public class JoinUserWithCourseManager
{
    static JoinUserWithCourseDao joinDao = App.getInstance().getDaoSession().getJoinUserWithCourseDao();

    /**
     * 批量添加关联表信息
     * @param joinUserWithCourses
     */
    public static void addJoinUserWithCoutses(List<JoinUserWithCourse> joinUserWithCourses){
        joinDao.insertOrReplaceInTx(joinUserWithCourses);
    }

    public static long addJoinUserWithCoutse(JoinUserWithCourse joinUserWithCourse){
        return joinDao.insertOrReplace(joinUserWithCourse);
    }

    public static void updateJoinUserWithCoutse(JoinUserWithCourse joinUserWithCourse){
        joinDao.update(joinUserWithCourse);
    }

    public static void deleteJoin(Long uId){
        joinDao.queryBuilder().where(JoinUserWithCourseDao.Properties.UId.eq(uId)).buildDelete().executeDeleteWithoutDetachingEntities();
    }

    public static JoinUserWithCourse queryById(Long joinId){
        return joinDao.queryBuilder().where(JoinUserWithCourseDao.Properties.Id.eq(joinId)).build().unique();
    }

    /**
     * 根据课程id和用户id查询关联信息
     * @param courseId
     * @param uId
     */
    public static JoinUserWithCourse queryJoinUserWithCoutse(Long courseId,Long uId){
        List<JoinUserWithCourse> list = joinDao.queryBuilder().where(JoinUserWithCourseDao.Properties.CId.eq(courseId),
                JoinUserWithCourseDao.Properties.UId.eq(uId)).list();
        if(list==null||list.size()==0)
            return null;
        return list.get(0);
    }
}
